package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Service.CheckItemService;
import com.itheima.comstant.MessageConstant;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/CheckItemController")
public class CheckItemController {


    @Reference
    private CheckItemService checkItemService;


    //添加检测项的代码
    @RequestMapping("/addCheckItem")
    public Result  addCheckItem(@RequestBody  CheckItem checkItem){
        try{
            Result result = checkItemService.addCheckItem(checkItem);
            return result;

        }catch(Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);

        }


    }

    //分页展示检测项的代码
    @RequestMapping("/findPage")
    public PageResult  findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult=checkItemService.findPage(queryPageBean);
        return pageResult;


    }



    //按id删除检测项
    @RequestMapping("/deleteById")
    public Result deleteById(int row){

        try{
            checkItemService.deleteById(row);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);

        }catch (RuntimeException r){
            return new Result(false,r.getMessage());

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);

        }

    }


    //编辑回显的方法
    @RequestMapping("/findByIdToEdit")
    public Result findByIdToEdit(int row){

        try{
            CheckItem checkItem= checkItemService.findByIdToEdit(row);
            return new Result(true,"找到对应的检测项",checkItem);

        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"获取数据失败，请刷新当前页面");

        }


    }



    //添加编辑修改
    @RequestMapping("/editCheckItem")
    public Result editCheckItem(@RequestBody CheckItem checkItem){


        try{
            checkItemService.editCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            return  new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }





    }


    //查询所有的检测项
    @RequestMapping("/findAllCheckItem")
    public Result findAllCheckItem(){
        List<CheckItem> list=checkItemService.findAllCheckItem();
        if(list!=null&&list.size()>0){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }else{
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }



}
